<template>
  <a-row :wrap="false">
    <a-col flex="200px">
      <RouterLink to="/">
        <div class="title-bar">
          <img class="logo" src="../assets/logo.png" alt="logo"/>
          <div class="title">HY映像</div>
        </div>
      </RouterLink>
    </a-col>
    <a-col flex="auto">
      <a-menu
        v-model:selectedKeys="current"
        mode="horizontal"
        :items="items"
        @click="doMenuClick"
      />
    </a-col>
    <a-col flex="120px">
      <div class="user-login-status">
        <div v-if="userLoginStore.loginUser.id">
          <a-dropdown>
            <ASpace>
              <a-avatar :src="userLoginStore.loginUser.userAvatar"/>
              {{ userLoginStore.loginUser.userName ?? '游客' }}
            </ASpace>
            <template #overlay>
              <a-menu>
                <a-menu-item v-if="userLoginStore.loginUser.id" @click="getUserInfo">
                  <IdcardOutlined/>
                  个人中心
                </a-menu-item>
                <a-menu-item>
                  <router-link to="/my_space">
                    <UserOutlined/>
                    我的空间
                  </router-link>
                </a-menu-item>
                <a-menu-item @click="goGitee">
                  <GitlabFilled/>
                  Git仓库
                </a-menu-item>
                <a-menu-item @click="doLogout">
                  <LogoutOutlined/>
                  退出登录
                </a-menu-item>
              </a-menu>
            </template>
          </a-dropdown>
        </div>

        <div v-else>
          <a-button type="primary" href="/picture/user/login">登录</a-button>
        </div>
      </div>
    </a-col>
  </a-row>
</template>
<script lang="ts" setup>
import {computed, h, ref} from 'vue'
import {HomeOutlined, IdcardOutlined, LogoutOutlined, UserOutlined, GitlabFilled} from '@ant-design/icons-vue'
import {MenuProps, message} from 'ant-design-vue'
import {useRouter} from 'vue-router'
import {useLoginUserStore} from '@/stores/useLoginUserStore.ts'
import {userLogoutUsingPost} from '@/api/userController.ts'

const userLoginStore = useLoginUserStore()
// 菜单列表
const originItems = [
  {
    key: '/',
    icon: () => h(HomeOutlined),
    label: '主页',
    title: '主页',
  },
  {
    key: '/add_picture',
    label: '创建图片',
    title: '创建图片',
  },
  {
    key: '/admin/pictureManage',
    label: '图片管理',
    title: '图片管理',
  },
  {
    key: '/admin/userManage',
    label: '用户管理',
    title: '用户管理',
  },
  {
    key: '/admin/spaceManage',
    label: '空间管理',
    title: '空间管理',
  },
  {
    key: 'other',
    label: h('a', { href: 'https://www.zhydoc.com', target: '_blank' }, '我的博客'),
    title: '我的博客',
  },
]

// 过滤菜单项
const filterMenus = (menus = [] as MenuProps['items']) => {
  return menus?.filter((menu) => {
    if (menu.key.startsWith('/admin') || menu.key.startsWith('/add_picture')) {
      const loginUser = userLoginStore.loginUser
      if (!loginUser || loginUser.userRole !== 'admin') {
        return false
      }
    }
    return true
  })
}

// 展示在菜单的路由数组
const items = computed<MenuProps['items']>(() => filterMenus(originItems))

const router = useRouter()
// 点击导航条进行路由跳转
const doMenuClick = ({key}) => {
  router.push({
    path: key,
  })
}
// 当前显示的高亮菜单
const current = ref<string[]>([])
// 根据路由跳转高亮菜单
router.afterEach((to, from, next) => {
  current.value = [to.path]
})
// 用户注销
const doLogout = async () => {
  const res = await userLogoutUsingPost()
  //console.log(res)
  if (res.data.code === 0) {
    userLoginStore.setLoginUser({
      userName: '未登录',
    })
    message.success('退出登录成功')
    await router.push('/user/login')
  } else {
    message.error('退出登录失败，' + res.data.message)
  }
}

const getUserInfo = () => {
  router.push('/user/getUserInfo')
}

const goGitee = () => {
  window.open('https://gitee.com/zhyzyy2121', '_blank')
}
</script>
<style scoped>
.title-bar {
  display: flex;
  align-items: center;
}

.title {
  color: black;
  font-size: 18px;
  margin-left: 16px;
}

.logo {
  height: 48px;
}
</style>
